package org.ns.learn.rpc.codec;

import com.alibaba.fastjson.JSON;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

import java.nio.charset.Charset;

public class RpcMessageResponseEncode extends MessageToByteEncoder<RpcResponse> {
    @Override
    protected void encode(ChannelHandlerContext ctx, RpcResponse msg, ByteBuf out) throws Exception {
        byte[] bytes = JSON.toJSONString(msg).getBytes(Charset.forName("UTF-8"));
        int length = bytes.length;

        out.writeInt(length);
        out.writeBytes(bytes);

    }
}
